package com.code.view;

import java.io.File;
import java.io.IOException;
import java.util.Hashtable;

import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.preference.PreferenceStore;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import com.code.view.entity.PreferenceVO;
import com.code.view.entity.TableItemControl;


/**
 * 代码生成器工具类
 *
 *
 * @author 朱国军
 * @version 2015年2月9日 朱国军
 */
public class ViewUtils {

    /**
     * 根据图片名称获取图片
     *
     * @param imageName 图片名称
     * @return 图片对象
     */
    public static Image getImage(String imageName) {
        Image image = Activator.getImageDescriptor("icons/" + imageName).createImage();
        return image;
    }


    /**
     * 获取首选项配置文件存放路径
     *
     * @return 首选项配置文件存放路径
     */
    public static String getPreferenceStoreFile() {
        return Activator.getDataFolder() + "setting.properties";
    }


    /**
     * 获取默认的代码输出路径：workspace下的output目录
     *
     * @return 默认的代码输出路径
     */
    public static String getDefaultsOutPutPath() {
        String workSpacePath = Platform.getInstanceLocation().getURL().getPath();
        workSpacePath.replace("\\", "/");
        return workSpacePath + "output";
    }


    /**
     * 读取首选项配置值，如果没有配置文件取默认值
     *
     * @return 首选项配置VO
     */
    public static PreferenceVO readPreferencePropertys() {
        String propertyFilePath = ViewUtils.getPreferenceStoreFile();
        File propertyFile = new File(propertyFilePath);
        if (!propertyFile.exists()) {
            return getDefaultsPreference();
        } else {
            PreferenceStore preferenceStore = new PreferenceStore(ViewUtils.getPreferenceStoreFile());
            try {
                preferenceStore.load();
                PreferenceVO preferenceVO = new PreferenceVO();
                preferenceVO.setDbUserName(preferenceStore.getString(ViewConstant.FIELD_NAME_USERNAME));
                preferenceVO.setDbPassword(preferenceStore.getString(ViewConstant.FIELD_NAME_PASSWORD));
                preferenceVO.setDbUrl(preferenceStore.getString(ViewConstant.FIELD_NAME_DBURL));
                preferenceVO.setDbDriver(preferenceStore.getString(ViewConstant.FIELD_NAME_DBDRIVER));
                preferenceVO.setAuthor(preferenceStore.getString(ViewConstant.FIELD_NAME_AUTHOR));
                preferenceVO.setEntitySuffix(preferenceStore.getString(ViewConstant.FIELD_NAME_ENTITY_SUFFIX));
                preferenceVO.setCommonPackage(preferenceStore.getString(ViewConstant.FIELD_NAME_COMMON_PACKAGE));
                preferenceVO.setPackagePath(preferenceStore.getString(ViewConstant.FIELD_NAME_PACKAGEPATH));
                preferenceVO.setOutPutPath(preferenceStore.getString(ViewConstant.FIELD_NAME_OUTPUTPATH));
                return preferenceVO;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }


    /**
     * 读取首选项配置值，如果没有配置文件取默认值
     *
     * @return 首选项配置VO
     */
    public static String readPackagePathValue() {
        PreferenceVO preferenceVO = readPreferencePropertys();
        if (preferenceVO != null) {
            return preferenceVO.getPackagePath();
        }
        return null;
    }


    /**
     * 获取默认首选项配置值
     *
     * @return 首选项VO
     */
    public static PreferenceVO getDefaultsPreference() {
        String outPutPathValue = ViewUtils.getDefaultsOutPutPath();
        if (outPutPathValue.startsWith("/")) {
            outPutPathValue = outPutPathValue.substring(1);
        }
        PreferenceVO preferenceVO = new PreferenceVO();
        preferenceVO.setDbUserName(ViewConstant.DEFALUTE_VALUE_USERNAME);
        preferenceVO.setDbPassword(ViewConstant.DEFALUTE_VALUE_PASSWORD);
        preferenceVO.setDbUrl(ViewConstant.DEFALUTE_VALUE_DBURL);
        preferenceVO.setDbDriver(ViewConstant.DEFALUTE_VALUE_DBDRIVER);
        preferenceVO.setAuthor(ViewConstant.DEFALUTE_VALUE_AUTHOR);
        preferenceVO.setPackagePath(ViewConstant.DEFALUTE_VALUE_PACKAGEPATH);
        preferenceVO.setEntitySuffix(ViewConstant.DEFALUTE_VALUE_ENTITY_SUFFIX);
        preferenceVO.setCommonPackage(ViewConstant.DEFALUTE_VALUE_COMMON_PACKAGE);
        preferenceVO.setOutPutPath(outPutPathValue);
        return preferenceVO;
    }


    /**
     * 根据路径创建该文件所在的文件夹，如果文件夹存在就不创建
     *
     * @param filePath 文件或文件夹路径
     * @param isFile 是否是文件：true 文件，false 文件夹
     */
    public static void createFolder(String filePath, boolean isFile) {
        File file = new File(filePath);
        File folder = null;
        if (!file.exists()) {
            if (isFile) {
                folder = new File(filePath.substring(0, filePath.lastIndexOf("/")));
            } else {
                folder = file;
            }
            if (!folder.exists() || !folder.isDirectory()) {
                folder.mkdirs();
            }
        }
    }


    /**
     * 创建数据库表设置表的一行
     *
     * @param item 行数据
     * @param table table对象
     * @param tableControls 表格控件
     */
    public static void createOneItemCells(TableItem item, Table table, Hashtable<TableItem, TableItemControl> tableControls) {
        // 复选框的控件
        TableEditor checkEditor = new TableEditor(table);
        Button checkButton = new Button(table, SWT.CHECK);
        String isCheck = item.getText(ViewConstant.CHECK_INDEX);
        if ("1".equals(isCheck)) {
            checkButton.setSelection(true);
        } else {
            checkButton.setSelection(false);
        }
        checkEditor.grabHorizontal = true;
        checkEditor.setEditor(checkButton, item, ViewConstant.CHECK_INDEX);

        // 数据库表名的控件
        TableEditor tableNameEditor = new TableEditor(table);
        Text tableNameText = new Text(table, SWT.BORDER);
        tableNameText.setText(item.getText(ViewConstant.TABLE_NAME_INDEX));
        tableNameEditor.grabHorizontal = true;
        tableNameEditor.setEditor(tableNameText, item, ViewConstant.TABLE_NAME_INDEX);

        // 包路径的控件
        TableEditor classPathEditor = new TableEditor(table);
        Text classPathText = new Text(table, SWT.BORDER);
        classPathText.setText(item.getText(ViewConstant.CLASS_PATH_INDEX));
        classPathEditor.grabHorizontal = true;
        classPathEditor.setEditor(classPathText, item, ViewConstant.CLASS_PATH_INDEX);

        TableItemControl tableItemControls =
                new TableItemControl(tableNameText, classPathText, checkButton, tableNameEditor, classPathEditor, checkEditor);

        tableControls.put(item, tableItemControls);

    }
}
